
local w,h = 700,400
local ui = class.panel:builder 
{
    _type = 'tooltip_backdrop0',
    x = (1920-w)/2,
    y = 200,
    w = w,
    h = h,
    alpha = 0.99,
}
local h1 = 30
local h2 = 80
local h3 = (ui.h - h1 - h2)/5
ui.speed = 20
ui.enter = ui:add_button('')
ui.title = ui:add_text('游戏胜利',0,10,ui.w,h1,20,4)
ui.tip = ui:add_text('点击隐藏',0,ui.h - h2,ui.w,h2,20,4)

local rows = {'玩家','伤害','金币','木材','击杀','装备'}
local size = {20,10,10,10,10,50}
local sum = 0
for _,num in ipairs(size) do
    sum = sum + num
end

ui.panels = {}
for a=0,4 do
    local panel = ui:add_panel('',0,h1 + h3*a,ui.w,h3)
    local row_x = 0
    ui.panels[a] = panel
    panel.rows = {}
    panel.items = {}
    for i,name in ipairs(rows) do
        local panel_w = size[i]/sum * panel.w
        if name=='装备' and a~=0 then
            local item_x = row_x + (panel_w - 40*6)/2
            local item_y = (panel.h - 40)/2
            for z=1,6 do
                local x1 = (z-1) * 40
                panel.items[z] = panel:add_button('1',item_x + x1,item_y,32,32)
            end
        else
            if a~=0 then
                if name=='玩家' then
                    name = ac.player[a]:get_color_name()
                else
                    name = '99999'
                end
            end
            panel.rows[i] = panel:add_text(name,row_x,0,panel_w,nil,12,4)
        end
        row_x = row_x + panel_w
    end
end

function ui:update(name)
    self.title:set_text(name)
    if self.timer1 then
        self.timer1:remove()
    end
    self.timer1 = game.loop(30,function(t)
        self:set_position(self.x,self.y + self.speed)
        if self.y>=200 then
            t:remove()
        end
    end)
    for a=1,4 do
        local panel = self.panels[a]
        local player = ac.player[a]
        local hero = player.hero
        panel.rows[2]:set_text(format_number1(player:get_data('总伤害',0)))
        panel.rows[3]:set_text(format_number1(player:get_data('总金币',0)))
        panel.rows[4]:set_text(format_number1(player:get_data('总木材',0)))
        panel.rows[5]:set_text(format_number1(player:get_data('总杀敌',0)))
        for a,button in ipairs(panel.items) do
            local item = hero and hero:find_item(a)
            button.skill = item
            if item then
                button:set_normal_image(item.art)
            else
                button:set_normal_image([[core\emptyinvslot1.blp]])
            end
        end
    end
end

function ui:on_button_clicked()
    if self.timer1 then
        self.timer1:remove()
    end
    self.timer1 = game.loop(30,function(t)
        self:set_position(self.x,self.y-self.speed)
        if self.y<-self.h then
            t:remove()
        end
    end)
end

function ui:on_button_mouse_enter(button)
    local skill = button.skill
    if skill and not skill.is_empty then
        new_item_tip(button,skill)
    end
end

ac.game:event '游戏-游戏胜利'(function(_,self)
    ui:update('|cff00ff00游戏胜利|r')
end)

ac.game:event '游戏-游戏失败'(function(_,self)
    ac.wait(100,function()
        ui:update('|cffff0000游戏失败|r')
    end)
end)